
package equipment_maintenance

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/equipment_maintenance"
    equipment_maintenanceReq "github.com/flipped-aurora/gin-vue-admin/server/model/equipment_maintenance/request"
)

type EquipmentMaintenanceService struct {}
// CreateEquipmentMaintenance 创建equipmentMaintenance表记录
// Author [yourname](https://github.com/yourname)
func (equipmentMaintenanceService *EquipmentMaintenanceService) CreateEquipmentMaintenance(ctx context.Context, equipmentMaintenance *equipment_maintenance.EquipmentMaintenance) (err error) {
	err = global.GVA_DB.Create(equipmentMaintenance).Error
	return err
}

// DeleteEquipmentMaintenance 删除equipmentMaintenance表记录
// Author [yourname](https://github.com/yourname)
func (equipmentMaintenanceService *EquipmentMaintenanceService)DeleteEquipmentMaintenance(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&equipment_maintenance.EquipmentMaintenance{},"id = ?",ID).Error
	return err
}

// DeleteEquipmentMaintenanceByIds 批量删除equipmentMaintenance表记录
// Author [yourname](https://github.com/yourname)
func (equipmentMaintenanceService *EquipmentMaintenanceService)DeleteEquipmentMaintenanceByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]equipment_maintenance.EquipmentMaintenance{},"id in ?",IDs).Error
	return err
}

// UpdateEquipmentMaintenance 更新equipmentMaintenance表记录
// Author [yourname](https://github.com/yourname)
func (equipmentMaintenanceService *EquipmentMaintenanceService)UpdateEquipmentMaintenance(ctx context.Context, equipmentMaintenance equipment_maintenance.EquipmentMaintenance) (err error) {
	err = global.GVA_DB.Model(&equipment_maintenance.EquipmentMaintenance{}).Where("id = ?",equipmentMaintenance.ID).Updates(&equipmentMaintenance).Error
	return err
}

// GetEquipmentMaintenance 根据ID获取equipmentMaintenance表记录
// Author [yourname](https://github.com/yourname)
func (equipmentMaintenanceService *EquipmentMaintenanceService)GetEquipmentMaintenance(ctx context.Context, ID string) (equipmentMaintenance equipment_maintenance.EquipmentMaintenance, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&equipmentMaintenance).Error
	return
}
// GetEquipmentMaintenanceInfoList 分页获取equipmentMaintenance表记录
// Author [yourname](https://github.com/yourname)
func (equipmentMaintenanceService *EquipmentMaintenanceService)GetEquipmentMaintenanceInfoList(ctx context.Context, info equipment_maintenanceReq.EquipmentMaintenanceSearch) (list []equipment_maintenance.EquipmentMaintenance, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&equipment_maintenance.EquipmentMaintenance{})
    var equipmentMaintenances []equipment_maintenance.EquipmentMaintenance
    // 如果有条件搜索 下方会自动创建搜索语句
    if len(info.CreatedAtRange) == 2 {
     db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
    }
    
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&equipmentMaintenances).Error
	return  equipmentMaintenances, total, err
}
func (equipmentMaintenanceService *EquipmentMaintenanceService)GetEquipmentMaintenancePublic(ctx context.Context) {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
